/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.dianlan.bits;

import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.component.gateway.core.util.HexUtil;
import info.peigen.hotpot.component.gateway.dianlan.bits.base.BitParserBase;
import info.peigen.hotpot.component.gateway.dianlan.facade.enums.DianlanWaterCoolingUnitWorkingMode;
import info.peigen.hotpot.component.gateway.dianlan.protocol.passive.DianlanWaterCoolingUnitStatusDataGram;

/**
 * <b>(DianlanWaterCoolingUnitStatusDataGramReasonBits)</b>
 * <p>
 * 水冷机组状态报文解析
 *
 * @author LiDaHai
 * @version 1.0.0
 * @since 2023/1/12
 */
public class DianlanWaterCoolingUnitStatusDataGramReasonBits extends BitParserBase {

    public DianlanWaterCoolingUnitStatusDataGram parseReason(String hexString) {
        String[]                              hexStrArray = hexStrToArray(hexString);
        DianlanWaterCoolingUnitStatusDataGram info        = DianlanWaterCoolingUnitStatusDataGram.builder().build();
        String                                bitString   = HexUtil.hexToBinary(hexStrArray[0]);
        int                                   offset      = 0;
        info.setReserve1(StrUtil.sub(bitString, offset, offset += 6) + "");
        info.setWorkingMode(parseWorkingMode(StrUtil.sub(bitString, offset, offset += 2)));
        info.setOutletWaterTemperature(parseInt(hexStrArray[1]) + (-40));
        info.setReturnWaterTemperature(parseInt(hexStrArray[2]) + (-40));
        info.setReserve2(hexStrArray[3] + "");
        info.setReserve3(hexStrArray[4] + "");
        info.setDemandRealTimePowerLowByte(0.1d * parseInt(hexStrArray[5]));
        info.setDemandRealTimePowerHighByte(0.1d * parseInt(hexStrArray[6]));
        bitString = HexUtil.hexToBinary(hexStrArray[7]);
        offset = 0;
        info.setFaultLevel(StrUtil.sub(bitString, offset, offset += 2));
        info.setFaultCode(parseInt(StrUtil.sub(bitString, offset, offset += 6)) + "");
        return info;
    }

    private DianlanWaterCoolingUnitWorkingMode parseWorkingMode(String bits) {
        switch (bits) {
            case "00":
                return DianlanWaterCoolingUnitWorkingMode.OffMode;
            case "01":
                return DianlanWaterCoolingUnitWorkingMode.CoolingMode;
            case "10":
                return DianlanWaterCoolingUnitWorkingMode.HeatingMode;
            case "11":
                return DianlanWaterCoolingUnitWorkingMode.WaterCirculationMode;
            default:
                return DianlanWaterCoolingUnitWorkingMode.UNKNOWN;
        }
    }

}